package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Store;
import cn.wolfcode.trip.base.mapper.StoreMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStoreService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreServiceImpl implements IStoreService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private UserMapper userMapper;


    public PageInfo<Store> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Store> list = storeMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(Store entity) {
        if (entity.getId() == null) {
            storeMapper.insert(entity);
        } else {
            storeMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        storeMapper.deleteByPrimaryKey(id);
    }

    public Store getStoreId(Long id) {
        return storeMapper.selectByPrimaryKey(id);
    }

    public Object buyAProducts(Long productId,Long userId) {
        Integer userScore = userMapper.getUserScoreById(userId);
        Store store = storeMapper.selectByPrimaryKey(productId);
        if(userScore<store.getScore()){
            throw new RuntimeException("您的智商不足,请充值");
        }
        userMapper.updateUserScore(userId,userScore-store.getScore());
        return null;
    }
}
